Page({
    data: {
        bankTypeList: [],
        currentBankType: null,
        name: '',
        nameErr: false,
        bankType: '',
        bankTypeErr: false,
        number: '',
        numberErr: false,
        buttonLoading: false,
        agreement: true, //同意协议
    },
    onLoad: function(options) {
        this.getBankType()
    },
    //获取开户银行类型
    getBankType() {
        this.request({
            url: '/supplier/account/bank'
        }, res => {
            this.setData({ bankTypeList: res.data })
        })
    },
    //切换开户银行类型
    onBankTypeChange({ detail }) {
        this.setData({
            currentBankType: detail.value,
            bankType: this.data.bankTypeList[detail.value].title
        })
    },
    //点击确定
    confirm() {
        const { name, currentBankType, number, agreement } = this.data
        if (currentBankType == null) return this.setData({ bankTypeErr: true })
        if (name === '') return this.setData({ nameErr: true })
        if (number === '') return this.setData({ numberErr: true })
        if (!agreement) return wx.showToast({
            title: '请阅读并同意用户协议',
            icon: 'none',
            duration: 2000
        })
        this.setData({ buttonLoading: true })
        let reqData = {
            account_name: name,
            bank_id: this.data.bankTypeList[this.data.currentBankType].bank_id,
            account_number: number
        }
        this.request({
            url: '/supplier/account/add',
            method: 'POST',
            data: reqData
        }, res => {
            wx.showToast({
                title: '添加成功',
                icon: 'success'
            })

            this.delay(() => {
                wx.navigateBack()
            })
        }, () => {
            this.setData({ buttonLoading: false })
        })
    },
    clearErr() {
        this.setData({ nameErr: false, bankTypeErr: false, numberErr: false })
    },
    //协议
    onAgreeChange({ detail }) {
        this.setData({ agreement: detail })
    }
})